home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10195 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  64 lines

  1. Path: tudelft.nl!news
  2. From: Ejo Schrama <schrama@geo.tudelft.nl>
  3. Newsgroups: comp.lang.c++
  4. Subject: Can somebody explain me how to do this in C++
  5. Date: 6 Mar 1996 15:30:32 GMT
  6. Organization: TU Delft, Faculty of Geodetic Engineering
  7. Message-ID: <4hkb2o$ko3@mo6.rc.tudelft.nl>
  8. NNTP-Posting-Host: dutgs7.geo.tudelft.nl
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.05 9000/735)
  13. X-URL: news:comp.lang.c++
  14.  
  15. Suppose you've got a base class containing protected data and several 
  16. public methods among which there are some virtual ones. This base class
  17. is inhereted to "lets call them" subclasses in which the virtual methods
  18. are implemented. All subclasses are defined during the initialization of a
  19. problem, however during the execution of a program "where all work is
  20. executed" you only want to deal with a linked list of base class objects
  21. which are accessed via a while loop. The code looks as follows:
  22.  
  23.   Cparam *localpointer = root_of_linked_list;
  24.   while (localpointer != NULL) {
  25.     localpointer->execute_some_virtual_method();
  26.     localpointer = localpointer->getlinktonext();
  27.   }
  28.  
  29. So far there is no problem until the following situation happens. There are 
  30. local variables declared as protected data inside the inhereted subclasses 
  31. (which don't exist in the baseclass) and I want to access those variables
  32. in the while construction described above. 
  33.  
  34. Any attempt I do to declare
  35.  
  36.   Cparam_inhereted_method *localpointer2 = localpointer; 
  37.  
  38. will always fail since:
  39.  
  40.   class Cparam {
  41.     protected:
  42.       <various data>
  43.     public:
  44.       <various constructors/destructors and methods except execute_routine>
  45.       virtual void execute_some_virtual_method();
  46.   };
  47.  
  48.   class Cparam_inhereted_method : public Cparam {
  49.     protected:
  50.       <various data>
  51.     public:
  52.       <constructors/destructors>
  53.       void execute_some_virtual_method();
  54.       void execute_routine();
  55.   };
  56.  
  57. so that I can never execute
  58.  
  59.   localpointer2->execute_routine();
  60.  
  61. The only alternative seems to introduce execute_routine to the Cparam
  62. base class (which I would like to avoid). How can I avoid this?
  63.  
  64.